﻿Imports lbGlobalcom
Imports Microsoft.Office.Interop
Public Class fProductos
    Dim daDatosTablas As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daTipoProd As lbGlobalcom.dsProductosTableAdapters.PRO_TIPOPRODUCTOTableAdapter
    Dim daTipoIva As lbGlobalcom.dsContabilidadTableAdapters.CON_TIPOIVATableAdapter
    Dim daProd As lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
    Dim lValida As Boolean = False
    Private Sub ExportProToExcel()
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim cex As New lbGlobalcom.cExcel(lbGlobalcom.cProducto.GetListaProductos())
            Dim m_Excel As Excel.Application
            m_Excel = cex.exportarExcel
            m_Excel.Visible = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub btnBuscaTipoProd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTipoProd.Click
        Dim fLTipoP As New fListaTipoProducto
        If fLTipoP.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDTIPOPRODUCTOTextBox.Text = fLTipoP.idTipoProd
        End If
    End Sub

    Private Sub IDTIPOPRODUCTOTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDTIPOPRODUCTOTextBox.TextChanged
        If IDTIPOPRODUCTOTextBox.Text.Trim.Length > 0 Then
            Me.lbTipoProducto.Text = daTipoProd.GetNombreTipo(IDTIPOPRODUCTOTextBox.Text)
        End If
    End Sub

    Private Sub fProductos_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If DsProductos.PRO_PRODUCTO.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay productos pendientes por salvar, desea salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    daProd.Update(DsProductos.PRO_PRODUCTO)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub
    
    Private Sub fProductos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daTipoProd = New lbGlobalcom.dsProductosTableAdapters.PRO_TIPOPRODUCTOTableAdapter
        daTipoIva = New lbGlobalcom.dsContabilidadTableAdapters.CON_TIPOIVATableAdapter
        daProd = New lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
        daDatosTablas = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            daDatosTablas.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "es")
            daDatosTablas.FillByIdGrupo(DsGlobalSubProd.GLO_DATOSTABLAS, "sp")
            DsProductos.PRO_PRODUCTO.Columns("TIPOSUBPROD").DefaultValue = 3
            DsProductos.PRO_PRODUCTO.Columns("ESTADO").DefaultValue = 1
            DsProductos.PRO_PRODUCTO.Columns("ESPADRE").DefaultValue = "N"
            DsProductos.PRO_PRODUCTO.Columns("ICCIDENCOMPRAS").DefaultValue = "N"
            DsProductos.PRO_PRODUCTO.Columns("APLICARANGODTO").DefaultValue = "N"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub btnBuscaTipoIva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTipoIva.Click
        Dim fLTI As New fListaTipoIva
        If fLTI.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDTIPOIVATextBox.Text = fLTI.TipoIva
        End If
    End Sub

    Private Sub IDTIPOIVATextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDTIPOIVATextBox.TextChanged
        If IDTIPOIVATextBox.Text.Trim.Length > 0 Then
            Me.lbTipoIVA.Text = daTipoIva.getNombreTipoIva(IDTIPOIVATextBox.Text)
        End If
    End Sub

    Private Sub PRO_PRODUCTOBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRO_PRODUCTOBindingNavigatorSaveItem.Click
        Try
            PRO_PRODUCTOBindingSource.EndEdit()
            daProd.Update(DsProductos.PRO_PRODUCTO)
            MessageBox.Show("Se salvarón productos con exito", "Salva Productos", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Esta seguro que desea eliminar este producto", "Elimina Producto", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                PRO_PRODUCTOBindingSource.EndEdit()
                PRO_PRODUCTOBindingSource.Remove(PRO_PRODUCTOBindingSource.Current)
                daProd.Update(DsProductos.PRO_PRODUCTO)
                MessageBox.Show("Se elimino producto con exito", "Elimina Producto", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub btnBuscaProducto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProducto.Click
        Try
            Dim flArt As New fListaProductos
            If flArt.ShowDialog = Windows.Forms.DialogResult.OK Then
                If DsProductos.PRO_PRODUCTO.GetChanges IsNot Nothing Then
                    If MessageBox.Show("Hay productos pendientes por salvar, desea salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        daProd.Update(DsProductos.PRO_PRODUCTO)
                    End If
                End If
                daProd.FillByIdProducto(DsProductos.PRO_PRODUCTO, flArt.idArticulo)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                lValida = False
                IDCLASETextBox_Validating(Nothing, Nothing)
                IDMARCATextBox_Validating(Nothing, Nothing)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub
    Private Sub IniControles()
        lbClase.Text = ""
        lbMarca.Text = ""
        lbTipoIVA.Text = ""
        lbTipoProducto.Text = ""
    End Sub
    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            IniControles()
            lValida = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub btnBuscaMarca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaMarca.Click
        Dim fBMarcas As New fListaMarcas
        If fBMarcas.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDMARCATextBox.Text = fBMarcas.mMarca
            IDMARCATextBox_Validating(Nothing, Nothing)
        End If
    End Sub

    
    Private Sub GeneraCodigoProducto()
        If IDTIPOPRODUCTOTextBox.Text.Trim.Length > 0 And IDMARCATextBox.Text.Trim.Length > 0 Then
            Try
                Dim cGetCP As New cGetCodigoProducto(IDMARCATextBox.Text, IDTIPOPRODUCTOTextBox.Text)
                IDPRODUCTOTextBox.Text = cGetCP.CodigoProducto
                cGetCP = Nothing
            Catch ex As Exception
                IDPRODUCTOTextBox.Text = ""
                MessageBox.Show(ex.Message)
                lbGlobalcom.cError.SalvaError(ex, "Samuel")
            End Try
        End If
    End Sub
    
    Private Sub IDCLASETextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles IDCLASETextBox.Validating
        If IDCLASETextBox.Text.Trim.Length > 0 Then
            Try
                Dim cClas As New cClaseProducto(IDCLASETextBox.Text)
                lbClase.Text = cClas.DescripcionClase
            Catch ex As Exception
                e.Cancel = True
                lbClase.Text = ""
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub IDMARCATextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles IDMARCATextBox.Validating

        Try
            If IDMARCATextBox.Text.Trim.Length > 0 AndAlso IDTIPOPRODUCTOTextBox.Text.Trim.Length > 0 Then
                Dim cMar As New cMarcas(IDMARCATextBox.Text)
                lbMarca.Text = cMar.DescripcionMarca
                If lValida Then _
                    GeneraCodigoProducto()
                'If (DsProductos.PRO_PRODUCTO.Rows.Count > 0 AndAlso DsProductos.PRO_PRODUCTO.Rows(0).RowState = DataRowState.Added) Then
                '    GeneraCodigoProducto()
                'ElseIf DsProductos.PRO_PRODUCTO.Rows.Count = 0 Then
                '    GeneraCodigoProducto()
                'End If
            ElseIf IDTIPOPRODUCTOTextBox.Text.Trim.Length = 0 Then
                'e.Cancel = True
                'MessageBox.Show("Debe de seleccionar primero el tipo de producto", "Error selección", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
        Catch ex As Exception
            e.Cancel = True
            lbMarca.Text = ""
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaIdClase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaIdClase.Click
        Dim fBClase As New fListaClases
        If fBClase.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDCLASETextBox.Text = fBClase.mClase
            IDCLASETextBox_Validating(Nothing, Nothing)
        End If
    End Sub

    Private Sub PRO_PRODUCTOBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PRO_PRODUCTOBindingSource.PositionChanged
        Try
            IDCLASETextBox_Validating(Nothing, Nothing)
            IDMARCATextBox_Validating(Nothing, Nothing)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
        
    End Sub

    Private Sub TIPOSUBPRODListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TIPOSUBPRODListBox.SelectedIndexChanged
        If TIPOSUBPRODListBox.SelectedValue <> 3 Then
            CANTIDADSUBPRODTextBox.ReadOnly = False
            CANTIDADSUBPRODTextBox.Text = 1
        Else
            CANTIDADSUBPRODTextBox.ReadOnly = True
            CANTIDADSUBPRODTextBox.Text = 0
        End If
    End Sub

   
    Private Sub btnExporToExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExporToExcel.Click
        Try
            ExportProToExcel()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

   
    Private Sub btnBuscaProdConv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProdConv.Click
        Try
            Dim fListaProd As New fListaProductos
            If fListaProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDPRODCONVIERTETextBox.Text = fListaProd.idArticulo
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub IDPRODCONVIERTETextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles IDPRODCONVIERTETextBox.Validating
        Try
            If IDPRODCONVIERTETextBox.Text.Trim.Length > 0 Then
                Dim iProd As New cProducto(IDPRODCONVIERTETextBox.Text)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            e.Cancel = True
        End Try
    End Sub
End Class